package com.wuliu.action.app;

import java.io.IOException;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.wuliu.action.BaseAction;
import com.wuliu.model.order.Order;
import com.wuliu.model.order.OrderDetail;
import com.wuliu.util.BaseUtil;
import com.wuliu.util.DbUtil;

//订单相关接口
public class AppSyncOrderAction extends BaseAction {
	//查询订单号查询订单
	public String CheckOrderByOrderId()
	{
	String orderId = request.getParameter("Orderid");
	String showMsg = "";
	
	JSONObject jsonRet=new JSONObject();
	Order order=new Order();
	
	order.setId(orderId);
	
	if (BaseUtil.getStr(orderId).equals("")){
		showMsg="订单号不能为空";
		jsonRet.put("showMsg", showMsg);
	}else{
		String sql="SELECT * FROM WL_ORDER WHERE ID='"+orderId+"'";
		List<List<String>> it =DbUtil.getQueryList(sql, 25);
		jsonRet.put("id", it.get(0));
		jsonRet.put("shipperId", it.get(1));
		jsonRet.put("shipperName", it.get(2));
		jsonRet.put("shipperTel", it.get(3));
		jsonRet.put("consigneeName", it.get(4));
		
		
	}
	
	try {
		response.setCharacterEncoding("utf8");
		response.getWriter().print(jsonRet);
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	return null;
	}

	public String GetOrderBySentDate(){
		JSONObject jsonRet=new JSONObject();
		JSONArray jsonArray=new JSONArray();
		
		String showMsg="";
		/*获取时间，并将时间转换成long类型*/
		String sentdate1=BaseUtil.getStr(request.getParameter("sentDate1"));
		String sentdate2=BaseUtil.getStr(request.getParameter("sentDate2"));
		
		Long date1=Long.valueOf(sentdate1);
		Long date2=Long.valueOf(sentdate2);
        int len=(int) (date2-date1);
		for(int i=0;i<=len;i++){
			String sql="SELECT SHIPPER_ID,SHIPPER_NAME,SHIPPER_TEL WHERE SENT_DATE="+"'"+String.valueOf(date1)+"'";
			date1=date1+i;
			List<List<String>> list=DbUtil.getQueryList(sql, 3);
			for(List<String> it:list){
				JSONObject json=new JSONObject();
				json.put("shipperId", it.get(0));
				json.put("shipperName", it.get(1));
				json.put("shipperTel", 2);
				jsonArray.add(json);
			}
			jsonRet.put("OrderList", jsonArray);
			
		}
		try {
			response.setCharacterEncoding("utf8");
			response.getWriter().print(jsonRet);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
		
	}
	/*
	 * 下定单
	 * 入参：
	 * 返回：
	 * 
	 * */
	public String PlaceOrder(){
		
		/*返回值json*/
		JSONObject jsonRet=new JSONObject();
		JSONObject jsonRet1=new JSONObject();
		JSONArray jsonArray=new JSONArray();
		JSONArray jsonArray2=new JSONArray();
		
		String showMsg="";
		
		/*获取app数据 */
		String shipperId=BaseUtil.getStr(request.getParameter("shipperId"));
		String id=BaseUtil.getStr(request.getParameter("id"));
		String shipperName=BaseUtil.getStr(request.getParameter("shipperName"));
		String shipperTel=BaseUtil.getStr(request.getParameter("shipperTel"));
		String shipperAddre=BaseUtil.getStr(request.getParameter("shipperAddre"));
		
		String consigneeId=BaseUtil.getStr(request.getParameter("consigneeId"));
		String consigneeName=BaseUtil.getStr(request.getParameter("consigneeName"));
		String consigneeTel=BaseUtil.getStr(request.getParameter("consigneeTel"));
		String consigneeAddre=BaseUtil.getStr(request.getParameter("consigneeAddre"));
		
		String sentDate=BaseUtil.getStr(request.getParameter("sentDate"));
		String receiveDate=BaseUtil.getStr(request.getParameter("receiveDate"));
		
		String checkStatus=BaseUtil.getStr(request.getParameter("checkStatus"));
		String orderStatus="0";                                                                  //0未验单
		
		String companyId=BaseUtil.getStr(request.getParameter("companyId"));
		String companyName=BaseUtil.getStr(request.getParameter("companyName"));
		
		String goodsName=BaseUtil.getStr(request.getParameter("goodsName"));
		
		String fee="0";                                                                            //下订单运费默认值为0
		String transferType=BaseUtil.getStr(request.getParameter("transferType"));
		String payType=BaseUtil.getStr(request.getParameter("payType"));
		String payPeople=BaseUtil.getStr(request.getParameter("payPeople"));
		String feeOnTransfer=BaseUtil.getStr(request.getParameter("feeOnTransfer"));
		String totalFee=BaseUtil.getStr(request.getParameter("totalFee"));
		
		String isInsure=BaseUtil.getStr(request.getParameter("isInsure"));
		String isInsurance=BaseUtil.getStr(request.getParameter("isInsurance"));
		String checker=BaseUtil.getStr(request.getParameter("checker"));
		
		String carId=BaseUtil.getStr(request.getParameter("carId"));
		String payCode=BaseUtil.getStr(request.getParameter("payCode"));
		
		Date date=new Date();
		DateFormat format=new SimpleDateFormat("yyyyMMddhhmmssSSS");
		String time=format.format(date);
		Random random = new Random(100000);//指定种子数字
		int n1 = random.nextInt(100000);
		id = "hy"+time+n1+id;
		
		String payFeeType="0";                                                                     //默认未付款
		String totalFeeStatus="0";
		
		String sendType=BaseUtil.getStr(request.getParameter("sendType"));
		
		/*获取app中货物的信息*/
		
		String goodsVolume=BaseUtil.getStr(request.getParameter("goodsVolume"));
		String goodsNum=BaseUtil.getStr(request.getParameter("goodsNum"));
		String goodsWeight=BaseUtil.getStr(request.getParameter("goodsWeight"));
		String goodsFee=BaseUtil.getStr(request.getParameter("goodsFee"));
		
		
		/*将获取的数据存入订单*/
		Order order=new Order();
		order.setId(id);
		order.setShipperId(shipperId);
		order.setShipperName(shipperName);
		order.setShipperTel(shipperTel);
		order.setShipperAddre(shipperAddre);
		
		order.setConsigneeId(consigneeId);
		order.setConsigneeName(consigneeName);
		order.setConsigneeTel(consigneeTel);
		order.setConsigneeAddre(consigneeAddre);
		
		order.setSentDate(sentDate);
		order.setReceiveDate(receiveDate);
		
		order.setCheckStatus(checkStatus);
		order.setOrderStatus(orderStatus);
		
		order.setCompanyId(companyId);
		order.setCompanyName(companyName);
		
		order.setGoodsName(goodsName);
		
		order.setFee(fee);
		order.setTransferType(transferType);
		order.setPayType(payType);
		order.setPayPeople(payPeople);
		order.setFeeOnTransfer(feeOnTransfer);
		order.setTotalFee(totalFee);
		
		order.setIsInsure(isInsure);
		order.setIsInsurance(isInsurance);
		order.setChecker(checker);
		
		order.setCarId(carId);
		order.setPayFeeType(payFeeType);
		order.setTotalFeeStatus(totalFeeStatus);
		
		order.setSendType(sendType);
		
		/*将获取的货物信息存入orderDetail*/
		OrderDetail orderDetail=new OrderDetail();
		orderDetail.setOrderId(id);
		orderDetail.setGoodsName(goodsName);
		orderDetail.setGoodsVolume(goodsVolume.equals("")? 0:Integer.valueOf(goodsVolume));
		orderDetail.setGoodsNum(goodsNum.equals("")?0:Integer.valueOf(goodsNum));
		orderDetail.setGoodsWeight(goodsWeight.equals("")?0:Double.valueOf(goodsWeight));
		
		orderDetail.setGoodsFee(goodsFee.equals("")? BigDecimal.valueOf(0):BigDecimal.valueOf(Double.valueOf(goodsFee)));
		
		
		/*判定下单是否成功，成功返回下单成功并且将数据存入数据库，否则返回下单失败*/
		if(BaseUtil.getStr(shipperId).equals("")){
			showMsg="发货人不能为空";
			
		}else if(BaseUtil.getStr(consigneeName).equals("")){
			showMsg="收货人不能为空";
		}else if(BaseUtil.getStr(goodsName).equals("")){
			showMsg="货物不能为空";
		}else {
			String sql1="INSERT INTO WL_ORDER(ID,SHIPPER_ID,SHIPPER_NAME,SHIPPER_TEL,SHIPPER_ADDRE,CONSIGNEE_ID,CONSIGNEE_NAME,"+
		"CONSIGNEE_TEL,CONSIGNEE_ADDRE,SENT_DATE,CHECK_STATUS,ORDER_STATUS,COMPANY_ID,"+
					"COMPANY_NAME,GOODS_NAME,FEE,TRANSFER_TYPE,PAY_TYPE,PAY_PEOPLE,FEE_ON_TRANSFER,TOTAL_FEE,"
							+ "IS_INSURE,IS_INSURANCE,CHECKER,CAR_ID,PAY_CODE,PAY_FEE_TYPE,TOTAL_FEE_STATUS,SEND_TYPE)"+"VALUES(?,?,?,?,?,?,?,?,?,"+
					"unix_timestamp(now()),?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
			List<String> it1=new ArrayList<String>();
			
			it1.add(order.getId());
			it1.add(order.getShipperId());
			it1.add(order.getShipperName());
			it1.add(order.getShipperTel());
			it1.add(order.getShipperAddre());
			
			it1.add(order.getConsigneeId());
			it1.add(order.getConsigneeName());
			it1.add(order.getConsigneeTel());
			it1.add(order.getConsigneeAddre());
			
			it1.add(order.getCheckStatus());
			it1.add(order.getOrderStatus());
			
			it1.add(order.getCompanyId());
			it1.add(order.getCompanyName());
			
			it1.add(order.getGoodsName());
			it1.add(order.getFee());
			it1.add(order.getTransferType());
			it1.add(order.getPayType());
			it1.add(order.getPayPeople());
			it1.add(order.getFeeOnTransfer());
			it1.add(order.getTotalFee());
			
			it1.add(order.getIsInsure());
			it1.add(order.getIsInsurance());
			it1.add(order.getChecker());
			it1.add(order.getCarId());
			it1.add(order.getPayCode());
		
			it1.add(order.getPayFeeType());
			it1.add(order.getTotalFeeStatus());
			
			it1.add(order.getSendType());
			
			String sql2="INSERT INTO WL_ORDER_DETAIL(ORDER_ID,GOODS_NAME,GOODS_VOLUME,GOODS_NUM,GOODS_WEIGHT,GOODS_FEE) VALUES (?,?,?,?,?,?)";
            List<Object> it2=new ArrayList<Object>();
				it2.add(orderDetail.getOrderId());
				it2.add(orderDetail.getGoodsName());
				it2.add(orderDetail.getGoodsVolume());
				it2.add(orderDetail.getGoodsNum());
				it2.add(orderDetail.getGoodsWeight());
				it2.add(orderDetail.getGoodsFee());
				
				jsonArray2.add(orderDetail);
			

			boolean result2=DbUtil.excuSQL2(it2, sql2);
			
			boolean result1=DbUtil.excuSQL(it1, sql1);
			
			String result=DbUtil.excuSQL1(it1, sql1);
			if(result2&result1){
				showMsg="下单成功";
			}else{
				showMsg="下单失败";
				System.out.println(result1);
				System.out.println(result);
			}
			//showMsg = result;
		}
		
      
		jsonRet1.put("order", order);
		jsonRet1.put("orderDetail", jsonArray2);
		
        jsonRet.put("showMsg", showMsg);
		jsonRet.put("json", jsonRet1);
		
		
		try {
			response.setCharacterEncoding("utf-8");
			response.getWriter().print(jsonRet);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return null;
		
	}
	
	public String PlaceOrderDetail(){
		/*返回值json*/
		JSONObject jsonRet=new JSONObject();
		
		String showMsg="";
		
		/*获取app数据 */
		
		String orderId=BaseUtil.getStr(request.getParameter("orderId"));
		
		String goodsName=BaseUtil.getStr(request.getParameter("goodsName"));
		String goodsVolume=BaseUtil.getStr(request.getParameter("goodsVolume"));
		String goodsNum=BaseUtil.getStr(request.getParameter("goodsNum"));
		String goodsWeight=BaseUtil.getStr(request.getParameter("goodsWeight"));
		String goodsFee=BaseUtil.getStr(request.getParameter("goodsFee"));
		
		
		
		/*获取app中货物的信息*/
		
		
		
		/*将获取的货物信息存入orderDetail*/
		
		OrderDetail orderDetail=new OrderDetail();
		orderDetail.setOrderId(orderId);
		orderDetail.setGoodsName(goodsName);
		orderDetail.setGoodsVolume(goodsVolume.equals("")? 0:Integer.valueOf(goodsVolume));
		orderDetail.setGoodsNum(goodsNum.equals("")?0:Integer.valueOf(goodsNum));
		orderDetail.setGoodsWeight(goodsWeight.equals("")?0:Double.valueOf(goodsWeight));
		orderDetail.setGoodsFee(goodsFee.equals("")? BigDecimal.valueOf(0):BigDecimal.valueOf(Double.valueOf(goodsFee)));
		
		
		/*判定下单是否成功，成功返回下单成功并且将数据存入数据库，否则返回下单失败*/
		
			if(orderId.equals("")){
				showMsg="orderId不能为空";
			}else{
			
			String sql2="INSERT INTO WL_ORDER_DETAIL(ORDER_ID,GOODS_NAME,GOODS_VOLUME,GOODS_NUM,GOODS_WEIGHT,GOODS_FEE) VALUES (?,?,?,?,?,?)";
			List<Object> it2=new ArrayList<Object>();
			
			it2.add(orderId);
			it2.add(orderDetail.getGoodsName());
			it2.add(orderDetail.getGoodsVolume());
			it2.add(orderDetail.getGoodsNum());
			it2.add(orderDetail.getGoodsWeight());
			it2.add(orderDetail.getGoodsFee());
		
			
			boolean result=DbUtil.excuSQL2(it2, sql2);
			
				
 
			if(result){
				showMsg="Ok";
			}else{
				showMsg="fail";
			}
		jsonRet.put("showMsg", showMsg);
		
		jsonRet.put("orderDetail", orderDetail);
		
		try {
			response.setCharacterEncoding("utf-8");
			response.getWriter().print(jsonRet);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
			}
		return null;
	}
}